//index.js
const app = getApp();
const request = require('../../utils/request.js');
const config = require('../../config.js');
var appkey = require('../../appkey.js');
var login = require('../../actions/login.js');

Page({
  data: {
    userInfo: {},
    shopinfo: {},
    goods: [],
    goodsNews: [],//最新商品
    hasMore: true,
    start: 1,
    size: 10,
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    hasToken:false
  },

  onShow:function(){
    //token 判断
    if (wx.getStorageSync('token').length > 0) {
      this.setData({ hasToken: true })
    } else {
      this.setData({ hasToken: false })
    }
  },

  onLoad: function (options) {
    var self = this;
    this.getAppUserInfo();

    wx.showLoading({
      title: '正在加载',
      mask: true,
    });
      app.getShopDetail()
      .then(data => {
        console.log(data);
        self.setData({
          shopinfo: app.globalData.shopInfo,
        });

        self.getGoodsAction();
        self.getNewGoodsAction();
        self.stopLoadingAction();
      }).catch(msg => {
        self.stopLoadingAction();
      });
  },


  stopLoadingAction: function () {
    wx.hideLoading();
  },

  getAppUserInfo: function (e) {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },

  //去支付
  payAction: function () {
    wx.navigateTo({
      url: '../pay/Pay',
    })
  },

  // 加载更多
  loadMoreAction: function () {
    var index = this.data.start;
    index = index + 1;
    this.setData({
      start: index
    })
    this.getGoodsAction();
  },


  getNewGoodsAction: function () {
    var url = config.goodsnewListUrl;
    var params = {
      shopId: app.globalData.shopId,
    }
    request.requestPost(url, params)
      .then(data => {
        console.log('goodsNews')
        console.log(data);
        this.setData({
          goodsNews: data
        })
      }).catch(msg => {
        console.log(data);
      });
  },

  getGoodsAction: function () {
    var url = config.goodsListUrl;
    var params = {
      shopId: app.globalData.shopId,
      page: this.data.start,
      size: this.data.size,
    }
    console.log(params);
    request.requestPost(url, params)
      .then((result) => {
        console.log('result');
        console.log(result);
        var datalist = this.data.goods;
        if (this.data.start == 1) {
          datalist = result;
        } else {
          result.map(item => {
            datalist.push(item);
          })
        }
        this.setData({
          hasMore: datalist.length == this.data.size,
          goods: datalist
        });
        console.log(this.data.goods);
      }).catch((message) => {
        console.log('error');
        console.log(message);
      });
  },

  /**
   * 商品详情
   */
  goodsDetailAction: function (e) {
    var goodsId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../goods/detail/goodsdetail?id=' + goodsId,
    });
  },


  onPullDownRefresh: function () {
    this.setData({
      start: 1
    })
    this.getGoodsAction();
  },

  getUserInfo: function () {
    wx.navigateTo({
      url: '/pages/regist/regist',
    })
  }
});
